﻿Imports System.Text

Public Class clsReader
    Private _IP As String
    Private _PORT As String
    Private _QR_CODE As String
    Private _Caller As Form
    Private _TEXT_BOX As DevExpress.XtraEditors.TextEdit
    Private _PLC As HslCommunication.Profinet.Siemens.SiemensS7Net
    Private _address As String
    Public Property IP As String
        Get
            Return _IP
        End Get
        Set(value As String)
            _IP = value
        End Set
    End Property

    Public Property PORT As String
        Get
            Return _PORT
        End Get
        Set(value As String)
            _PORT = value
        End Set
    End Property

    Public Property Caller As Form
        Get
            Return _Caller
        End Get
        Set(value As Form)
            _Caller = value
        End Set
    End Property

    Public Property QR_CODE As String
        Get
            Return _QR_CODE
        End Get
        Set(value As String)
            _QR_CODE = value
        End Set
    End Property

    Public Property TEXT_BOX As DevExpress.XtraEditors.TextEdit
        Get
            Return _TEXT_BOX
        End Get
        Set(value As DevExpress.XtraEditors.TextEdit)
            _TEXT_BOX = value
        End Set
    End Property

    Dim intReaderNum As Integer = 1
    Protected WithEvents reader1 As clsEquipCooperateCommand

    Public Sub New(__IP As String, __PORT As String, __caller As Form, __TEXT_BOX As Object, __PLC As HslCommunication.Profinet.Siemens.SiemensS7Net, __address As String)
        Me.IP = __IP
        Me.PORT = __PORT
        Me.Caller = __caller
        Me.TEXT_BOX = __TEXT_BOX
        Me._PLC = __PLC
        Me._address = __address
        reader1 = New clsEquipCooperateCommand(IP, PORT)
        reader1.CommandConnect()
        AddHandler reader1.tcpevent, AddressOf TCPCLEvent1
        System.Threading.Thread.Sleep(500)
    End Sub
    Delegate Sub setTextBoxDelegate(QRcode As String)
    Delegate Sub TCPCLEventDelegate1(e As TCPListener.TCPEventArgs)
    Private Sub TCPCLEvent1(sender As System.Object, e As TCPListener.TCPEventArgs)
        Debug.WriteLine("TCPCLEvent in")
        If reader1.isConnected Then
            Dim adrData As New TCPCLEventDelegate1(AddressOf DoTCPCLEvent1)
            _Caller.BeginInvoke(adrData, e)
        End If
    End Sub
    Private Sub setTextBox(QRcode As String)
        _TEXT_BOX.Text = QRcode

        If IsNothing(_PLC) = False AndAlso _PLC.ConnectServer.IsSuccess Then
            Dim dltype As String = QRcode.Substring(QRcode.Length - 1)
            Dim intDLtype As Integer
            If dltype.ToUpper = "S" Then intDLtype = 1
            If dltype.ToUpper = "M" Then intDLtype = 2
            If dltype.ToUpper = "L" Then intDLtype = 3

            _PLC.Write(_address, intDLtype)
            '_PLC.Write("DB10000.DBX2.0", 0)
            'objPLCDB.insertLog(cmb_Type.Text, txtGJBXH.Text, "setDL1", "DB10000.DBX0.0：" & intDLtype)
        End If
    End Sub
    Public Sub read()
        'MsgBox(reader1.isConnected)
        reader1.CommandSend("start")
    End Sub
    Private Sub DoTCPCLEvent1(e As TCPListener.TCPEventArgs)
        ' 接続完了
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Connect Then
            Debug.WriteLine("接続完了")
        End If
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Shutdown Then
            Debug.WriteLine("接続切断")
        End If
        ' データ受信
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Receive Then
            Debug.WriteLine("データ受信")
            Dim receiveDataStr = Encoding.ASCII.GetString(e.receiveData, 0, e.receiveData.Count())
            '<P>TP002</P>
            If receiveDataStr.Equals("<p>NoRead</p>") = False Then
                Dim receiveDataStrReplace = receiveDataStr.Replace("<p>", "").Replace("</p>", "")
                'txtDL_NoA.Text = String.Empty
                QR_CODE = receiveDataStrReplace
                Dim setTextHander As New setTextBoxDelegate(AddressOf setTextBox)
                _Caller.Invoke(setTextHander, QR_CODE)
                If _PLC.Write("DB30.DBX20.0", True).IsSuccess = False Then
                    _PLC.Write("DB30.DBX20.0", True)
                    If _PLC.Write("DB30.DBX20.1", False).IsSuccess = False Then
                        _PLC.Write("DB30.DBX20.1", False)
                    End If
                End If
                '    objPLCDB.insertLog(cmb_Type.Text, txtGJBXH.Text, "DoTCPCLEvent1", "receiveDataStr：" & receiveDataStrReplace)

                '    '把吊篮类型设置到PLC
                '    If jxbPLC_CONNECTED = True Then
                '        Dim dltype As String = receiveDataStrReplace.Substring(receiveDataStrReplace.Length - 1)
                '        Dim intDLtype As Integer
                '        If dltype.ToUpper = "S" Then intDLtype = 1
                '        If dltype.ToUpper = "M" Then intDLtype = 2
                '        If dltype.ToUpper = "L" Then intDLtype = 3

                '        jxbPLC.Write("DB10000.DBX0.0", intDLtype)
                '        jxbPLC.Write("DB10000.DBX2.0", 0)
                '        objPLCDB.insertLog(cmb_Type.Text, txtGJBXH.Text, "DoTCPCLEvent1", "DB10000.DBX0.0：" & intDLtype)

                '    End If
            Else
                If intReaderNum < 21 Then
                    intReaderNum += 1
                    reader1.CommandSend("start")
                Else
                    If _PLC.Write("DB30.DBX20.0", False).IsSuccess = False Then
                        _PLC.Write("DB30.DBX20.0", False)
                        If _PLC.Write("DB30.DBX20.1", True).IsSuccess = False Then
                            _PLC.Write("DB30.DBX20.1", True)
                        End If
                    End If
                End If
                '    For index = 0 To 4
                '        reader1.CommandSend("start")
                '        objPLCDB.insertLog(cmb_Type.Text, txtGJBXH.Text, "DoTCPCLEvent1", " reader1.CommandSend：start")
                '    Next
            End If

        End If

        ' Exception
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Exception Then
            Debug.WriteLine("Exception受信")
            Debug.WriteLine(e.exception.Message)
            'SetStatus1(False)
            'CommandReceive(Nothing)
        End If
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Timeout Then
            Debug.WriteLine("timeout受信")
            'SetStatus1(False)
            'CommandReceive(Nothing)
        End If
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Cancel Then
            Debug.WriteLine("Cancel受信")
            'SetStatus1(False)
            'CommandReceive(Nothing)
        End If
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Retry Then
            Debug.WriteLine("retry受信")
            'm_cEquipCommandControl.CommandDisConnect()
        End If
    End Sub
End Class
